const puppeteer = require('puppeteer');
const path = require('path');
const express = require('express');
const app = express();
app.use(express.json())
const url2pdf = app.get('/tools/url2pdf', async (req, res) => {
    console.log('reqs:',req.query)
    const { url } = req.query;
    const browser = await puppeteer.launch({
        args: ['--no-sandbox', '--disable-setuid-sandbox', '--disable-dev-shm-usage'],
        ignoreHTTPSErrors: true,
        headless: true,
    });
    const page = await browser.newPage();
    // ,{waitUntil:['load']}
    await page.goto(url,{ waitUntil: 'networkidle2' });
    // await page.waitForSelector('body');
    await page.waitForNetworkIdle();
    // const outputPath = path.join(process.cwd() , 'files/pdf.pdf')
    // path: outputPath,  指定文件输出路径
    const pdf = await page.pdf({ format: 'A4' });
    await browser.close();
    console.log('---------------------success!!!!!')
    res.set({
        "Content-Type": "application/pdf",
        "Content-Length": pdf.length,
    })
    res.send(pdf);
})

module.exports = url2pdf;